import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('views.erp.sale.customer.name'),
      rules: 'required',
      componentProps: {
        placeholder: $t('views.erp.sale.customer.namePlaceholder'),
      },
    },
    {
      fieldName: 'contact',
      label: $t('views.erp.common.contact'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.contactPlaceholder'),
      },
    },
    {
      fieldName: 'mobile',
      label: $t('views.erp.common.mobile'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.mobilePlaceholder'),
      },
    },
    {
      fieldName: 'telephone',
      label: $t('views.erp.common.telephone'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.telephonePlaceholder'),
      },
    },
    {
      fieldName: 'email',
      label: $t('views.erp.common.email'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.emailPlaceholder'),
      },
    },
    {
      fieldName: 'fax',
      label: $t('views.erp.common.fax'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.faxPlaceholder'),
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'sort',
      label: $t('views.common.sort'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.common.sortPlaceholder'),
        precision: 0,
      },
      rules: 'required',
    },
    {
      fieldName: 'taxNo',
      label: $t('views.erp.common.taxNo'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.taxNoPlaceholder'),
      },
    },
    {
      fieldName: 'taxPercent',
      label: $t('views.erp.common.taxPercent'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.common.taxPercentPlaceholder'),
        precision: 2,
      },
      rules: z.number().min(0).max(100).optional(),
    },
    {
      fieldName: 'bankName',
      label: $t('views.erp.common.bankName'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.bankNamePlaceholder'),
      },
    },
    {
      fieldName: 'bankAccount',
      label: $t('views.erp.common.bankAccount'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.bankAccountPlaceholder'),
      },
    },
    {
      fieldName: 'bankAddress',
      label: $t('views.erp.common.bankAddress'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.bankAddressPlaceholder'),
      },
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
        rows: 3,
      },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.erp.sale.customer.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.sale.customer.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'mobile',
      label: $t('views.erp.common.mobile'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.mobilePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'email',
      label: $t('views.erp.common.email'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.emailPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('views.erp.sale.customer.name'),
      minWidth: 150,
    },
    {
      field: 'contact',
      title: $t('views.erp.common.contact'),
      minWidth: 120,
    },
    {
      field: 'mobile',
      title: $t('views.erp.common.mobile'),
      minWidth: 130,
    },
    {
      field: 'telephone',
      title: $t('views.erp.common.telephone'),
      minWidth: 130,
    },
    {
      field: 'email',
      title: $t('views.erp.common.email'),
      minWidth: 180,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'sort',
      title: $t('views.common.sort'),
      minWidth: 80,
    },
    {
      field: 'remark',
      title: $t('common.remark'),
      minWidth: 150,
      showOverflow: 'tooltip',
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
